package kuang.future;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class FutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        long time1 = System.currentTimeMillis();
        FutureTask<Integer> futureTask_1 = new FutureTask(() -> {
            TimeUnit.SECONDS.sleep(5);
            return 1024;
        });
        new Thread(futureTask_1).start();

        FutureTask<Integer> futureTask_2 = new FutureTask(() -> {
            TimeUnit.SECONDS.sleep(3);
            return 1024;
        });
        new Thread(futureTask_2).start();
        //可以判断，main线程异常结束了，另外两个线程还没结束。
        //另外两个线程不依赖于主线程
        System.out.println(futureTask_2.get(1, TimeUnit.SECONDS));
        System.out.println("---------------------");
        System.out.println(futureTask_1.get() + futureTask_2.get());
        System.out.println(System.currentTimeMillis() - time1);
    }
}
